package com.fenghongzhang.kotlin1903.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase


@Database(entities = [User::class,Student::class],version = 2)
abstract class UserDataBase : RoomDatabase(){
    abstract fun getUserDao():UserDao

    abstract fun getStudentDao():StudentDao
    //单例的另一种固定写法
    companion object{
        private var instant: UserDataBase? = null

        @Synchronized
        fun getDatabase(context: Context):UserDataBase{
            instant?.let {
                return it
            }

            return Room.databaseBuilder(context.applicationContext,UserDataBase::class.java,"app_database")
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration() //删除所有库,重新创建, 如果不这么做.要写sql语句升级
                .build().apply {
                    instant = this
                }
        }
    }
}